package hmvictor.jpql;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;
import org.openide.filesystems.FileChooserBuilder;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.WindowManager;

@ActionID(
        category = "Edit",
        id = "hmvictor.jpql.SaveAction"
)
@ActionRegistration(
        displayName = "#CTL_SaveAction"
)
@ActionReference(path = "Menu/File", position = 1200)
@Messages("CTL_SaveAction=Save")
public final class SaveAction implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        JPQLEditorTopComponent c = (JPQLEditorTopComponent) WindowManager.getDefault().findTopComponent("JQPLEditorTopComponent");
        File file = c.getFile();
        if (file == null) {
            File home = new File(System.getProperty("user.home"));
            file = new FileChooserBuilder("user-dir").setTitle("Save File").setDefaultWorkingDirectory(home).setApproveText("Save").showSaveDialog();
        }
        if(file != null) {
            c.saveContent(file);
        }
    }

}
